package com.vf.admin.miaodong.controller;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppHelp;
import com.vf.admin.miaodong.service.AfterHelpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 帮助中心
 *
 * @author anxiang
 */
@CrossOrigin
@Controller
@RequestMapping("/after/help")
public class AfterHelpController extends BaseController {

    @Autowired
    AfterHelpService afterHelpService;

    /**
     * 获取帮助中心
     */
    @RequestMapping("/list")
    public String list(SplitPageBean splitPageBean) {
        try {
            Map<String, Object> map = new HashMap<>(16);
            List<AppHelp> helpList = afterHelpService.findList(map, splitPageBean);
            request.setAttribute("list", helpList);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/help/list");
        } catch (Exception e) {
            return this.error("系统异常");
        }
    }

    /**
     * 帮助中心添加跳转
     *
     * @return
     */
    @RequestMapping("/add")
    public String add() {
        try {
            return this.sucess("/web/after/help/add");
        } catch (Exception e) {
            return this.error("系统异常");
        }
    }

    @RequestMapping("/del")
    @ResponseBody
    public Result del(Integer id) {
        afterHelpService.delete(id);
        return sucessJSON("成功");
    }

    /**
     * 帮助中心编辑跳转
     *
     * @param id id
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Integer id) {
        try {
            AppHelp help = afterHelpService.get(id);
            request.setAttribute("help", help);
            return this.sucess("/web/after/help/edit");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    /**
     * 添加帮助中心
     *
     * @param help 实体类
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(AppHelp help) {
        try {
            if (StringUtils.isEmpty(help.getTitle())) {
                return errorJSON("标题不能为空");
            }
            if (StringUtils.isEmpty(help.getContent())) {
                return errorJSON("内容不能为空");
            }
            if (help.getSort() == null) {
                return errorJSON("排序不能为空");
            }
            if (help.getType() == null) {
                return errorJSON("类型不能为空");
            }
            afterHelpService.saveOrUpdate(help);
            return sucessJSON("添加成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }


}
